#coding: utf-8
require 'mechanize'
require 'iconv'
require 'hpricot'
#gem 'mail', '2.4.4'  
require 'mail'  
require 'redis'

class String
  def gsub_html
    self.gsub(/\r|\t|\n/,"").gsub(/<(\S*?)[^>]*>.*?|<.*? \/>/,"").gsub("&nbsp;","").strip
  end
end

 def email_notice
      puts "send_email...."
      title = "gk66 #{@cn_city} cookie过期"
      desp = "账号:#{@contact} \n cookie 已过期 请更新! \n http://123.196.117.24:9090/home/show "
      Mail.defaults do
          delivery_method :smtp, address: "smtp.126.com", port: 25 ,:user_name  => 'xulei324@126.com',:password   => 'adssdds',:enable_ssl => true
      end
	  ["xuyanchao@51kkww.com","xuyanchao2008@163.com","wangyingxiong@51kkww.com"].each do |n|
      #["msn324@qq.com","651151255@qq.com"].each do |n|
      begin
      	sleep(5)
      	puts "send to #{n}"
        Mail.deliver do
           from    'xulei324@126.com'
           to      n
           subject  title
           body     desp
          #add_file '/full/path/to/somefile.png'
        end
      rescue Exception => e
      	puts e
      end
      end
  end

def get_list(arr,url)
	begin
		sleep(2)
		str = @a.get(url).body
		doc = Hpricot(Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",str).join)
		if doc.to_s.match(/对不起/)
			puts "cookie已过期！"
			return 0
		end

		puts max_count = doc.search("div[@class=pager]").to_s.match(/共 (\d+?) 条/)[1].to_s.to_i rescue 1

		puts max_page = max_count/20 + 1

		for p in 1..max_page
			sleep(2)
			puts link = "#{url}&page=#{p}"
			doc = Hpricot(Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",@a.get(link).body).join) if p!=1
			if doc.to_s.match(/对不起/)
				puts "cookie已过期！"
				return 0
			end
			doc.search("table[@id=tablecloth]").search("tr").each_with_index do |n,i|
				next if i==0
				puts s1 = n.search("td")[0].to_s.gsub_html
				puts s2 = n.search("td")[1].to_s.gsub_html
				puts s3 = n.search("td")[2].to_s.gsub_html
				puts s4 = n.search("td")[3].to_s.gsub_html
				puts s5 = n.search("td")[4].to_s.gsub_html
				puts s6 = n.search("td")[5].to_s.gsub_html
				puts s7 = n.search("td")[6].to_s.gsub_html
				puts s8 = n.search("td")[7].to_s.gsub_html
				puts s9 = n.search("td")[8].to_s.gsub_html
				puts s10 = n.search("td")[9].to_s.gsub_html
				puts s11 = n.search("td")[10].to_s.gsub_html
				puts s12 = n.search("td")[11].to_s.gsub_html
				puts s13 = n.search("td")[12].to_s.gsub_html
				File.open("#{@city}_items.txt","a+") do |file|
					file.puts "#{arr[0]},#{arr[1]},#{arr[2]},#{arr[3]},#{arr[4]},#{arr[5]},#{arr[6]},#{arr[7].to_s},,#{s1},#{s2},#{s3},#{s4},#{s5},#{s6},#{s7},#{s8},#{s9},#{s10},#{s11},#{s12},#{s13}"
				end
				puts "-"*160
			end
		end
	rescue Exception => e
		puts e
	end
	
end

# @file = Roo::Excel.new("urls.xls") 

# @file.default_sheet = @file.sheets.first 


def run
	@city = "2014hubei"
	@cn_city = "湖北"
	@contact = "37846123"
	@password = "285187"

	@a = Mechanize.new
	@redis= Redis.new(:host => 'xxx.xxx.xxx.xxx', :port => '6379', :db => 3)
	#buffer = File.open("cookies.txt","r").readlines.join
	
	i = 0
	while true
		s = @redis.spop("gk66_urls_#{@contact}")
		if s == nil
			break
		end
		arr = s.split(",")
		#url = "http://14wj.gk66.cn/wj/zy_dt.aspx?t=9E3140EC52CD79603EF8D45AAB1EFBC7BFA4808220E10C79&zy=7676CD9D18892C84&p=&n=E3A09360D2AA5594&w=E28304031B769A23&b=BE0F36FA13AD428A&zyids=FD2835C1CC2AF9F3&wc1=&wc2=&fs1=&fs2="
		puts url = "http://14wj.gk66.cn#{arr[7]}"
		begin
			buffer = @redis.get("#{@contact}")
			@a.cookie_jar.load_cookiestxt(StringIO.new(buffer))
			puts "index:#{i}"
			#if arr[0].to_s.match("2014")
				status = get_list(arr,url)
				puts "status=>#{status}"
				if status == 0
					email_notice					
					for inx in 1..300
						puts "程序休眠中，(#{300-inx})秒后继续...."
						sleep(1)
					end
					raise ArgumentError 
				end
			#end
		rescue ArgumentError
			retry
		end
		i+=1
	end

end



run


